/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.resourcestore.utils.tag;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.components.Component;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 分页逻辑Bean
 * 
 * @author
 */
public class Pages extends Component {

	private HttpServletRequest request;

	private String pageNo;
	private String total;           //所有总数
	private String styleClass;
	private String theme;
	private String includes; 
 

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getIncludes() {
		return includes;
	}

	public void setIncludes(String includes) {
		this.includes = includes;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getPageNo() {
		return pageNo;
	}

	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public Pages(ValueStack arg0, HttpServletRequest request) {
		super(arg0);
		this.request = request;
	}

	@Override
	public boolean start(Writer writer) {

		boolean result = super.start(writer);
		try {
			StringBuilder str = new StringBuilder();
			Map cont = this.getStack().getContext();
			StrutsRequestWrapper req = (StrutsRequestWrapper) cont
					.get(StrutsStatics.HTTP_REQUEST);

			String url = (String) req
					.getAttribute("javax.servlet.forward.request_uri");
			String params = req.getQueryString();
			if(null!=params){
				params = params.replaceAll("&pagination.pageNo=(\\d{1,3})","");
			}
			
			// 从ValueStack中取出数值
			Object obj = this.getStack().findValue(pageNo);
			pageNo = String.valueOf((Integer) obj);////////////////////////////////////////////
			obj = this.getStack().findValue(total);
			total = String.valueOf((Integer) obj);

			StringBuffer perUrl = new StringBuffer("");
			if (includes != null) {
				String[] perm = includes.split(",");
				for (int i = 0; i < perm.length; i++) {
					String permName = perm[i];
					Object obje = this.getStack().findValue(permName);
					String vType = obje.getClass().toString();

					vType = vType.substring(vType.lastIndexOf(".") + 1, vType
							.length());
					perUrl.append("&" + permName);
					if (vType.equals("String")) {
						String tmp = (String) this.getStack().findValue(
								permName);
						perUrl.append("=" + tmp);
					} else if (vType.equals("Long")) {
						Long tmp = (Long) this.getStack().findValue(permName);
						perUrl.append("=" + tmp);
					} else if (vType.equals("Float")) {
						Float tmp = (Float) this.getStack().findValue(permName);
						perUrl.append("=" + tmp);
					} else if (vType.equals("Integer")) {
						Integer tmp = (Integer) this.getStack().findValue(
								permName);
						perUrl.append("=" + tmp);
					} else if (vType.equals("Boolean")) {
						Boolean tmp = (Boolean) this.getStack().findValue(
								permName);
						perUrl.append("=" + tmp);
					}
				}
			}

			Integer cpageInt = Integer.valueOf(pageNo);
			str.append("<span ");
			if (styleClass != null) {
				str.append(" class='" + styleClass + "'>");
			} else {
				str.append(">");
			}
			str.append("<table width='65%' border='0' align='center' cellpadding='0' cellspacing='0'>");
			str.append("<tr>");
			// 文本样式
			if (theme == null || "text".equals(theme)) {
				// 当前页与总页数相等
				if (pageNo.equals(total)||"0".equals(total)) {
					// 如果total = 1，则无需分页，显示“[第1页] [共1页]”
					if ("1".equals(total)||"0".equals(total)) {

					} else {
						// 到达最后一页,显示“[首页] [上一页] [末页]”
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo=1"
										+ perUrl
										+ "'><font class='luntanbmy'>首页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo="
										+ (cpageInt - 1)
										+ perUrl
										+ "'><font class='luntanbmy'>上一页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><font class='luntanbmy'>下一页</font></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><font class='luntanbmy'>最后一页</font></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td class='lqbz'>共<span style='color: red'>"
										+ total + "</span>页</td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td class='lqbz'>当前第<span style='color: red'>"
										+ pageNo + "</span>页</td>");
					}
				} else {
					// 当前页与总页数不相同
					if ("1".equals(pageNo)) {
						// 第一页，显示“[首页] [下一页] [末页]”
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><font class='luntanbmy'>首页</font></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><font class='luntanbmy'>上一页</font></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo="
										+ (cpageInt + 1)
										+ perUrl
										+ "'><font class='luntanbmy'>下一页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo="
										+ total
										+ perUrl
										+ "'><font class='luntanbmy'>最后一页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td class='lqbz'>共<span style='color: red'>"
										+ total + "</span>页</td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td class='lqbz'>当前第<span style='color: red'>"
										+ pageNo + "</span>页</td>");
					} else {
						// 不是第一页，显示“[首页] [上一页] [下一页] [末页]”
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo=1"
										+ perUrl
										+ "'><font class='luntanbmy'>首页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo="
										+ (cpageInt - 1)
										+ perUrl
										+ "'><font class='luntanbmy'>上一页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo="
										+ (cpageInt + 1)
										+ perUrl
										+ "'><font class='luntanbmy'>下一页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td><a href='"
										+ url
										+ "?"+params+"&pagination.pageNo="
										+ total
										+ perUrl
										+ "'><font class='luntanbmy'>最后一页</font></a></td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td class='lqbz'>共<span style='color: red'>"
										+ total + "</span>页</td>");
						str.append("<td>&nbsp;&nbsp;&nbsp; <br></td>");
						str
								.append("<td class='lqbz'>当前第<span style='color: red'>"
										+ pageNo + "</span>页</td>");
					}
				}
			}
			str.append("</tr>");
			str.append("</table>");
			str.append("</span>");

			writer.write(str.toString());

		} catch (IOException ex) {
			Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);
		}
		return result;
	}
}
